#include <iostream>
#include <memory>
#include "TcpServer.hpp"
#include "Translate.hpp"

static Dictionary dict;

void Usage()
{
    std::cout << "Please enter: ./Server [ip] port" << std::endl;
}

void Ping(std::string& str)
{
    str = "Pong";
}

void Transforme(std::string& str)
{
    for (auto& ch : str)
    {
        if ('a' <= ch && ch <= 'z')
        {
            ch -= 32;
        }
    }
}

void  Translate(std::string& str)
{
    std::string chinese = dict.Find(str);
    str = chinese;
}

int main(int argc, char* argv[])
{
    if (argc != 2 && argc != 3)
    {
        Usage();
        return 4;
    }
    std::string ip = argv[1];
    uint16_t port = atoi(argv[argc - 1]);
    if (argc == 2)
        ip = "";

    std::unique_ptr<TcpServer> t(new TcpServer(port, ip));
    t->AddFunc("ping", Ping);
    t->AddFunc("transforme", Transforme);
    t->AddFunc("translate", Translate);

    t->Start();

    return 0;
}